﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SnmpForm.APConfig
{
    public partial class ucSnmpTrust : UserControl
    {
        DbWrapper.DbDataTypes.Ap _info = null;
        public ucSnmpTrust()
        {
            InitializeComponent();
        }

        public void initWith(DbWrapper.DbDataTypes.Ap info)
        {
            _info = info.Clone();
            Update();
        }

        public bool Update(bool bView = true)
        {
            if (bView)
            {
                textBoxUsername.Text = _info.snmp_user;
                textBoxPassword.Text = _info.snmp_password;
            }
            else
            {
                _info.snmp_user = textBoxUsername.Text;
                _info.snmp_password = textBoxPassword.Text;
            }
            return true;
        }

        public bool CheckInput(ref string strErro)
        {
            bool bValid = true;
            string strPasswd = textBoxPassword.Text;
            if (strPasswd.Length>=8 && strPasswd.Length<=16)
            {
                //用户名是否含有非法字符
                bValid =
                    System.Text.RegularExpressions.Regex.IsMatch(strPasswd, @"^[a-zA-Z0-9]+$");
                if (bValid)
                {
                    bValid = true;
                }
                else
                {
                    strErro = "含有非法字符";
                    bValid = false;
                }
            }
            else
            {
                strErro = "密码长度必须大于8,且不大于16";
                bValid = false;
            }
            labelError.Text = strErro;
            return bValid;
        }

        public DbWrapper.DbDataTypes.Ap Get()
        {
            Update(false);
            return _info;
        }
    }
}
